package com.doudou.base.resources.controller;

import com.doudou.base.resources.service.AppClientDetailsService;
import com.doudou.base.resources.service.UserAccountInfoService;
import com.doudou.base.resources.service.UserDetailsInfoService;
import com.doudou.client.entity.AppClientDetailsDto;
import com.doudou.client.entity.UserAccountInfoDto;
import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 傻男人
 * @Date: 2020/6/2 10:06
 * @Version: 1.0
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@Api(value = "基本认证的接口",tags = "基本认证的接口")
public class BasicController{

    @Autowired
    protected AppClientDetailsService appClientDetailsService;

    @Autowired
    private UserAccountInfoService userAccountInfoService;

    @Autowired
    protected UserDetailsInfoService userDetailsInfoService;

    /**
     * 根据clientId查询
     * @param clientId
     * @return
     */
    @ApiOperation(value = "根据clientId查询",notes = "根据clientId查询")
    @ApiImplicitParam(name = "clientId",value="clientId",dataType="String",required = true,paramType = "path")
    @GetMapping(value = "/client/{clientId}/clientId111")
    @PreAuthorize("hasRole('ROLE_SYS')")
    public Result<AppClientDetailsDto> findByClientId(@PathVariable("clientId")String clientId) {
        AppClientDetailsDto result = appClientDetailsService.findClientByClientId(clientId);
        return Result.ok(result);
    }

    /**
     * 通过用户名查找用户信息
     * @param username
     * @return
     */
    @ApiOperation(value = "通过用户名查找用户信息",notes = "通过用户名查找用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value="用户名",dataType="String",required = true)})
    @GetMapping(value = "/account/username")
    @PreAuthorize("hasRole('ROLE_SYS')")
    public Result<UserAccountInfoDto> findByUsername(@RequestParam("username")  String username) {
        UserAccountInfoDto userAccountInfoDto = userAccountInfoService.findByUsername(username);
        return Result.ok(userAccountInfoDto);
    }

    /**
     * 通过手机号码查找用户信息，如没有则注册用户
     * @param phone 手机号码
     * @return
     */
    @ApiOperation(value = "通过手机号码查找用户信息，如没有则注册用户",notes = "通过手机号码查找用户信息，如没有则注册用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",value="手机号码",dataType="String",required = true)})
    @GetMapping(value = "/account/phone")
    @PreAuthorize("hasRole('ROLE_SYS')")
    public Result<UserAccountInfoDto> findNoRegisterByPhone(@RequestParam("phone")String phone) {
        UserAccountInfoDto userAccountInfoDto = userAccountInfoService.findNoRegisterByPhone(phone);
        return Result.ok(userAccountInfoDto);
    }

    /**
     * 根据userId查询用户的详细信息
     * @param userId   用户的id
     * @return
     */
    @ApiOperation(value = "根据id查询用户的详细信息",notes = "根据id查询用户的详细信息")
    @ApiImplicitParam(name = "userId",value="用户的id",dataType="Long",required = true,paramType = "path")
    @GetMapping(value = "/details/{userId}/userId")
    public Result<UserDetailsInfoDto> findUserDetailsInfoByUserId(@PathVariable("userId")Long userId) {
        UserDetailsInfoDto result = userDetailsInfoService.findUserDetailsInfoByUserId(userId);
        return Result.ok(result);
    }
}
